
var _bank = [];

$(function() {
	$.extend({
		format: function(str, step, splitor) {
			str = str.toString();
			var len = str.length;

			if (len > step) {
				var i = 0, s = "", arr = [];
				while (i < len) {
					s = str.substr(i, step);					
					arr.push(s);					
					i += step;					
				}				 
				 
				str = arr.join(splitor);
			 }
			
			 return str;
		}
	});
});

function supplier(iscustomer, supplier) {	
	if (iscustomer == '0') {
		var url = 'contact.php?do=Supplier.view&id=' + supplier;
		_dialog_view(url, '供应商', '650x420');
	}
	
	if (iscustomer == '1') {
		var url = 'contact.php?do=FinanceCustomer.view&id=' + supplier;
		_dialog_view(url, '客户', '650x420');
	}
}

function payment_info(s) {
	if (s == null || s == '-')　{
		return;
	} else if (s == '1') {
		payment_customer('2');
	} else if (s == '0') {
		payment_supplier();
	}
}

function payment_supplier() {
	// alert("supplier");
	
	Tip.supplier();

	$('#supplier').blur(function() {
		var a = $(this).val();
		Pay.supplier(a);
		
		var body = { };
		body.id = a;
		$.getJSON('contact.php?do=Supplier.ajax_id', body, function(json) {
			$("#c_name").val(json.name);
			$("#c_fullname").val(json.fullname);
			$("#c_address").val(json.address);
			$("#c_web").val(json.url);
		});
		
		var body = {};
		body.q = a;
		
		$.getJSON('finance.php?do=PaymentMy.ajax_linkman', body, function(json) {			
			Supplier.linkman(json);
		});
	});
}

function payment_customer(my) {
	// alert("customer");
	
	// Tip.supplier();

	if (my == '1') {
		url = 'contact.php?do=CustomerMy.ajax_my';
	} else {
		url = 'contact.php?do=CustomerMy.ajax_code';
	}
	
	$("input[id^='supplier']:visible").autocomplete({
		source: function( request, response ) {
			$.ajax({
				url: url,
				dataType: "json",
				data: {
					featureClass: "P",
					style: "full",
					maxRows: 12,
					q: request.term
				},
				success: function( data ) {
					response( $.map( data, function( item ) {
						return {
							label: item.code + " [" + item.name + ", " + item.emp_t + "]",
							value: item.id + ":" + item.code
						}
					}));
				}
			});
		},
		
		minLength: 1
	});

	$('#supplier').blur(function() {
		var a = $(this).val();
		Pay.customer(a);
		
		var body = { };
		body.id = a;
		$.getJSON('contact.php?do=CustomerMy.ajax_id', body, function(json) {
			// alert($.toJSON(json));
			$("#c_name").val(json.name);
			$("#c_fullname").val(json.fullname);
			$("#c_address").val(json.address);
			$("#c_web").val(json.url);
		});
		
		var body = {};
		body.q = a;
		
		$.getJSON('finance.php?do=PaymentMy.ajax_linkman1', body, function(json) {			
			Supplier.linkman(json);
		});
	});
}

var Pay = {
	supplier : function(a) {
		var body = {};
		body.q = a;

		$.getJSON('finance.php?do=PaymentMy.ajax_bank', body, function(json) {
			_bank = json;

			Pay.payment($('#payment').val());
		});

		$.getJSON('finance.php?do=PaymentMy.ajax_linkman', body, function(json) {
			_linkman = json;

			Pay.payment($('#payment').val());
			Pay.voucher($('#voucher').val());
		});
	},
	
	customer : function(a) {
		var body = {};
		body.q = a;

		$.getJSON('finance.php?do=PaymentMy.ajax_bank3', body, function(json) {
			// alert($.toJSON(json));
			_bank = json;

			Pay.payment($('#payment').val());
		});

		$.getJSON('finance.php?do=PaymentMy.ajax_linkman1', body, function(json) {
			_linkman = json;

			Pay.payment($('#payment').val());
			Pay.voucher($('#voucher').val());
		});
	},

	payment : function(a) {
		if (a == '1' || a == '3') {
			$("#label_payment_linkman").show();
			$("#label_payment_bank").hide();
			this.xj();
		} else if (a == '2' || a == '4') {
			$("#label_payment_linkman").hide();
			$("#label_payment_bank").show();
			this.zz();
		} else {
			$("#label_payment_linkman").hide();
			$("#label_payment_bank").hide();
			$("#payment_x").removeClass("ui-icon-circle-triangle-s").addClass("ui-icon-circle-triangle-w");
		}
	},

	zz : function() {
		
		$select = $("#payment_bank");
		$select.empty();
		var count = 0;
		$.each(_bank, function(k, v) {
			var a;
			if (v.id == '-') {
				a = v.name;
			} else {
				var s = v.code.replace(/\s+/g, "");
				a = v.name + "/" + s.right(4);
			}
			var item = new Option(a, v.id);
			$select[0].options.add(item);
			
			/*
			if (count == 1) {
				Pay._zz(v.id);
			}*/

			count++;
		});

		$select.change(function() {
			var id = $(this).val();
			Pay._zz(id);
		});

		$("#label_payment_bank").show();
		$("#payment_x").removeClass("ui-icon-circle-triangle-w").addClass("ui-icon-circle-triangle-s");
		
		var code = $("input[id^='x_code']:visible").val();
		var b = $.format(code.replace(/\s/g, ''), 4, ' ');
		$("#code_t").html(b);
		
		$("input[id^='x_code']:visible").keyup(function() {
			var s = $(this).val();
			// alert(s);
			var a = s.replace(/\s/g, '');
			
			var b = $.format(a, 4, ' ');
			//$(this).val(b);
			$("#code_t").html(b);
		});
	},

	_zz : function(id) {
		var url = 'finance.php?do=Bank.ajax_id&q=' + id;
		$.getJSON(url, function(json) {
			$("#x_bank").val(json.bank + json.branch);
			$("#x_name").val(json.name);
			$("#x_code").val(json.code);
			
			/*
			if (json.category == '2') {
				var swift = "swiftcode:{0}".format(json.swift)
				$("#x_content").val(swift);
			}*/
			
		});
	},

	xj : function() {
		$select = $("#payment_linkman");
		$select.empty();
		var count = 0;
		$.each(_linkman, function(k, v) {
			var item = new Option(v.name, v.id);
			$select[0].options.add(item);
/*
			if (count == 1) {
				Pay._xj(v.id);
			}*/

			count++;
		});

		$select.change(function() {
			var id = $(this).val();
			Pay._xj(id);
		});

		$("#label_payment_linkman").show();
		$("#payment_x").removeClass("ui-icon-circle-triangle-w").addClass("ui-icon-circle-triangle-s");
	},

	_xj : function(id) {
		var url = 'info.php?do=Contact.ajax_id&q=' + id;
		$.getJSON(url, function(json) {
			$("#z_name").val(json.name);
			$("#z_tel").val(json.tel);
			$("#z_mobile").val(json.mobile);
			$("#z_content").val(json.im);
		});
	},

	voucher : function(a) {
		if (a == '-' || a == '1') {
			$("#label_voucher").hide();
			return;
		}

		$("#label_voucher").show();

		$select = $("#voucher_t");
		$select.empty();
		$.each(_linkman, function(k, v) {
			var item = new Option(v.name, v.id);
			$select[0].options.add(item);
		});

		$select.change(function() {
			var id = $(this).val();
			var url = 'info.php?do=Contact.ajax_id&q=' + id;
			$.getJSON(url, function(json) {
				if (a == 2) {
					$("#voucher_x").val(json.email);
				}
				if (a == 3) {
					$("#voucher_x").val(json.fax);
				}
			});
		});
	},

	collapse : function(cc) {
		var a = $("#label_payment_linkman");
		var b = $("#label_payment_bank");

		var s = $('#payment').val();
		if (s == '1' || s == '3') {
			a.toggle();
			b.hide();
		} else if (s == '2' || s == '4') {
			b.toggle();
			a.hide();
		} else {
			alert("请选择支付方式，谢谢！");
			a.hide();
			b.hide();
		}

		if ($(cc).hasClass("ui-icon-circle-triangle-w")) {
			$(cc).removeClass("ui-icon-circle-triangle-w").addClass("ui-icon-circle-triangle-s");
		} else {
			$(cc).removeClass("ui-icon-circle-triangle-s").addClass("ui-icon-circle-triangle-w");
		}
	}
}
